package com.tms.control;

import com.tms.domain.index.*;
import com.tms.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 首页数据控制类
 */
@RestController
@RequestMapping("/api")
public class IndexController extends BaseController {

    @Autowired
    ToolService toolService;

    @Autowired
    ToolDefService toolDefService;

    @Autowired
    UseRecordService useRecordService;

    @Autowired
    RepairRecordService repairRecordService;

    @Autowired
    UserService userService;


    @GetMapping("/homePageData")
    public Response getIndexData() {
        //所有的tool数量
        int toolNum = toolService.findAllTools();
        //所有的toolDef数量
        int tDefNum = toolDefService.findAllToolDefNums();
        //所有的进出库记录数量
        int allUseRecordNum = useRecordService.findAllUseRecordNums();
        //今日进出库数量
        int todayUseRecordNum = useRecordService.findAllUseRecordNumsToday();
        //所有的维修记录数量
        int failureNum = repairRecordService.findAllRepairRecordNums();
        //故障率
        double failureRate = (double) failureNum / toolNum;
        //所有的报废记录
        int scrapNum = toolService.findAllScrap();
        //报废率
        double scrapRate = (double) scrapNum / (scrapNum + toolNum);
        ToolInfo toolInfo = new ToolInfo(toolNum, tDefNum);
        UseInfo useInfo = new UseInfo(allUseRecordNum, todayUseRecordNum);
        FailureInfo failureInfo = new FailureInfo(failureNum, failureRate);
        ScrapInfo scrapInfo = new ScrapInfo(scrapNum, scrapRate);
        //使用历史
        List<UseHistory> useHistory = useRecordService.getUseHistory();
        //toolDef对应的tool数量
        List<TDefUseRank> tDefUseRank = toolDefService.findTDefAndNum();
        //各种权限用户的数量
        List<UserInfo> userInfo = userService.getUserInfo();
        //tool的各种状态的数量
        List<ToolStatusInfo> toolStatusInfo = toolService.getToolStatusInfo();
        Data data = new Data(toolInfo, useInfo, failureInfo, scrapInfo, useHistory, tDefUseRank, userInfo, toolStatusInfo);
        return new Response("success", "成功", data);
    }
}
